source("https://gitee.com/ry2an/my_public_functions_r/raw/master/sql_in.R")
source("https://gitee.com/ry2an/my_public_functions_r/raw/master/judge_order.R")
library(stringr)
#### Method 1 ####

find_child_pre <- function(id_vec = c(), 
                           father_vec = c(), 
                           father_id = "not_ava", 
                           pre_father_name = c("no"),
                           remove_old_father = F){
  if(length(sql_not_in(test_vec = father_vec, in_vec = id_vec))> 0){
    if(remove_old_father){
      father_vec[sql_not_inl(test_vec = father_vec, in_vec = id_vec)] <- father_id
    }else{
      old_father <- sql_not_in(test_vec = father_vec, in_vec = id_vec)
      old_father <- old_father[sql_not_inl(test_vec = old_father, in_vec = pre_father_name)]
      # 这个unique是2021年11月18日加上去的，我是大侦探啊发现这么个错误，哈哈哈哈
      old_father <- unique(old_father)
      id_vec <- c(id_vec, old_father)
      father_vec <- c(father_vec, rep(father_id, length(old_father)))
      father_vec[sql_not_inl(test_vec = father_vec, in_vec = id_vec)] <- father_id
    }
  }
  return(list(id_vec, father_vec, father_id))
}

find_child <- function(id_vec = c(), 
                       father_vec = c(), 
                       father_id = "not_ava", 
                       name_code = "",
                       slient = T){
  # find father this level 
  this_level_ele <- id_vec[father_vec == father_id]
  if(length(this_level_ele) > 0){
    out_judge <- T
    if(slient == F){
      cat("We Get something this level: ", this_level_ele, "\n")
    }
  }else{
    if(slient == F){
      cat("We found NOTHING in this level!\n")
    }
    return(list(F))
  }
  
  # give name_code for this level
  name_code <- paste(name_code ,1:length(this_level_ele), ".", sep = "")
  if(slient == F){
    cat("We already give names for this level", name_code, "\n")
  }
  # get fatherren for this level element
  for(i in 1:length(this_level_ele)){
    if(slient == F){
      cat("We are looking children for", this_level_ele[i], "\n")
    }
    result <- find_child(id_vec = id_vec, 
                         father_vec = father_vec, 
                         father_id = this_level_ele[i],
                         name_code = name_code[i])
    if(result[[1]]){
      if(slient == F){
        cat("We found some childs for", this_level_ele[i], "\n", result[[2]], "\n", result[[3]], "\n")
      }
      this_level_ele <- c(this_level_ele, result[[2]])
      name_code <- c(name_code, result[[3]])
    }else{
      if(slient == F){
        cat("There is nothing we can add here.\n")
      }
    }
  }
  return(list(out_judge, this_level_ele, name_code))
}



get_level <- function(id_vec = c(), 
                      father_vec = c(),
                      pre_father_name = "no",
                      remove_old_father = F,
                      name_code = ""){
  pre_result <- find_child_pre(id_vec = id_vec, 
                               father_vec = father_vec, 
                               father_id = "not_ava", 
                               pre_father_name = pre_father_name,
                               remove_old_father = remove_old_father
  )
  result <- find_child(id_vec = pre_result[[1]],
                       father_vec = pre_result[[2]],
                       father_id = pre_result[[3]],
                       name_code = name_code)
  result_df <- data.frame(result[[2]], result[[3]])
  names(result_df) <- c("id", "title")
  order_vec <- judge_order(vec = result_df$title, desc = F)
  result_df <- result_df[order(order_vec),]
  result_df$level <- 0
  for(i in 1:dim(result_df)[1]){
    result_df$level[i] <- str_count(result_df$title[i], pattern = "\\.") 
    # cat(result_df$level[i], "\n")
  }
  return(result_df)
}


#### Test Area ####
# id_veci <- paste("a",1:10, sep = "")
# father_veci <- c("a3","a3", "noo", "a5", "a13", "a9", "no", "a7", "a2", "a6")
# 
# get_level(id_vec = id_veci,
#           father_vec = father_veci,
#           pre_father_name = c("no", "noo"),
#           remove_old_father = F,
#           name_code = "")
# 
# get_level(id_vec = id_veci,
#           father_vec = father_veci,
#           pre_father_name = c("no", "noo"),
#           remove_old_father = T,
#           name_code = "")


